package com.mindata.blockchain.socket.base;

import com.mindata.blockchain.socket.body.base.BaseBody;
import com.mindata.blockchain.socket.common.Const;
import com.mindata.blockchain.socket.packet.BlockPacket;
import jdk.nashorn.internal.ir.CallNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.utils.json.Json;

/**
 * 基础handler
 *
 * @author tanyaowu
 * 2017年3月27日 下午9:56:16
 */
public abstract class AbstractBlockHandler<T extends BaseBody> implements HandlerInterface {
    Logger logger = LoggerFactory.getLogger(AbstractBlockHandler.class);

    protected AbstractBlockHandler() {
    }

    public abstract Class<T> bodyClass();

    @Override
    public Object handler(BlockPacket packet, ChannelContext channelContext) throws Exception {
        String jsonStr;
        T bsBody = null;
        if (packet.getBody() != null) {
            jsonStr = new String(packet.getBody(), Const.CHARSET);
            bsBody = Json.toBean(jsonStr, bodyClass());
        }
//        logger.warn("数据中心body:{}", bsBody);
        return handler(packet, bsBody, channelContext);
    }

    /**
     * 实际的handler处理
     *
     * @param packet         packet
     * @param bsBody         解析后的对象
     * @param channelContext channelContext
     * @return 用不上
     * @throws Exception Exception
     */
    public abstract Object handler(BlockPacket packet, T bsBody, ChannelContext channelContext) throws Exception;

}
